﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaseTool
{
    /// <summary>
    /// 全局路径
    /// </summary>
    public static class PathTool
    {
        /// <summary> 打开机种的型号名 </summary>
        public static string ProductType = "default";
        /// <summary> debug位置 </summary>
        public static string BaseDirectory = AppDomain.CurrentDomain.BaseDirectory;
        /// <summary> 软件根目录(盘位置) </summary>
        public static string RootDirectory { get { return Path.GetPathRoot(BaseDirectory); } }


        //配置类-----------------------------------------------------------------------
        /// <summary> 配置文件根文件夹，路径Debug\Alltype\ </summary>
        public static string AllTypePath { get { return BaseDirectory + "Alltype\\"; } }
        /// <summary> 保存当前型号和型号列表的文件，路径Debug\Alltype\Alltype.xml </summary>
        private static string AllTypeXml { get { return AllTypePath + "Alltype.xml"; } }
        /// <summary> 全局参数文件，一般时保存MyGlobal.Config的，路径Debug\Alltype\MyGlobal.xml </summary>
        public static string MyGlobalXml { get { return AllTypePath + "MyGlobal.xml"; } }
        /// <summary> 当前型号参数文件，对应当前产品型号的路径，Debug\Alltype\ProductType\ </summary>
        public static string CurrentProductPath { get { return $"{AllTypePath}{ProductType}\\"; } }

        /// <summary> 棋盘格工具文件，Debug\Alltype\ProductType\CheckerboardImage.xml </summary>
        public static string CheckerboardImageXml { get { return $"{CurrentProductPath}CheckerboardImage.xml"; } }
        /// <summary> 条纹光栅工具文件，Debug\Alltype\ProductType\StripedGratingImage.xml </summary>
        public static string StripedGratingImageXml { get { return $"{CurrentProductPath}StripedGratingImage.xml"; } }
        /// <summary> 标定参数文件，Debug\Alltype\ProductType\CalibrationParam.xml </summary>
        public static string CalibrationParamXml { get { return $"{CurrentProductPath}CalibrationParam.xml"; } }
        /// <summary> 重建参数文件，Debug\Alltype\ProductType\ReconstructParam.xml </summary>
        public static string ReconstructParamXml { get { return $"{CurrentProductPath}ReconstructParam.xml"; } }

        //数据类-----------------------------------------------------------------------
        /// <summary> Data路径 Debug\Data\ </summary>
        public static string DataPath { get { return $"{BaseDirectory}Data\\"; } }
        /// <summary> 截图保存路径  Debug\Data\ScreenImage\ </summary>
        public static string DataScreenImagePath { get { return DataPath + "ScreenImage\\"; } }
        /// <summary> 条纹光栅路径  Debug\Data\PatternImage\ </summary>
        public static string DataPatternImagePath { get { return DataPath + "PatternImage\\"; } }
        /// <summary> 棋盘格路径  Debug\Data\CheckerBoardImage\ </summary>
        public static string DataCheckerBoardImagePath { get { return DataPath + "CheckerBoardImage\\"; } }
        /// <summary> 图像采集路径  Debug\Data\Capture\ </summary>
        public static string DataCapturePath { get { return DataPath + "Capture\\"; } }
        /// <summary>
        /// 标定路径
        /// </summary>
        public static string DataCaptureCalibrationPath(DateTime now)
        {
            return $"{DataCapturePath}{now.ToString("yyyyMMdd_HHmmss")}_Calibration\\";
        }
        /// <summary>
        /// 重建路径
        /// </summary>
        public static string DataCaptureReconstructPath(DateTime now)
        {
            return $"{DataCapturePath}{now.ToString("yyyyMMdd_HHmmss")}_Reconstruct\\";
        }

        /// <summary> 标定路径  Debug\Data\Calibration\ </summary>
        public static string DataCalibrationPath { get { return DataPath + "Calibration\\"; } }
        /// <summary> 重建路径  Debug\Data\Reconstruct\ </summary>
        public static string DataReconstructPath { get { return DataPath + "Reconstruct\\"; } }

        /// <summary> 一键打包文件夹，Debug\Data\PackageData </summary>
        public static string DataPackagePath { get { return DataPath + $"PackageData\\"; } }
        /// <summary> 一键打包临时数据文件夹，Debug\Data\PackageData\Temp </summary>
        public static string DataPackageTempPath { get { return DataPath + $"PackageData\\Temp\\"; } }

        //log类-----------------------------------------------------------------------
        /// <summary> 运行日志文件所在路径 Debug\Data\Log\</summary>
        public static string DataLogPath { get { return DataPath + "Log\\"; } }

        //读取保存型号-----------------------------------------------------------------------
        /// <summary>
        /// 读取当前配方名
        /// </summary>
        /// <returns></returns>
        public static void ReadProduct()
        {
            try
            {
                //不存在则新建
                if (!File.Exists(AllTypeXml))
                    SaveProduct();
                //存在文件则读取
                else
                    ProductType = (string)FileTool.ReadXML(AllTypeXml, typeof(string));
            }
            catch (Exception ex)
            {
                ProductType = "default";
                Log.SaveError(ex, $"读取当前配方名异常，{ex.Message}", LogType.Run);
            }
            finally
            {
                //创建当前配方文件夹
                if (!Directory.Exists(CurrentProductPath))
                    Directory.CreateDirectory(CurrentProductPath);
            }
        }

        /// <summary>
        /// 读取配方列表
        /// </summary>
        /// <returns></returns>
        public static string[] ReadProductList()
        {
            try
            {
                //不存在路径则返回空
                if (!Directory.Exists(AllTypePath))
                    return new string[0];

                //提取配方名字
                DirectoryInfo DI = new DirectoryInfo(AllTypePath);
                DirectoryInfo[] Product = DI.GetDirectories();
                List<string> DIname = new List<string>();
                foreach (DirectoryInfo name in Product)
                {
                    DIname.Add(name.Name);
                }
                return DIname.ToArray();
            }
            catch (Exception ex)
            {
                Log.SaveError(ex, $"读取配方列表异常，{ex.Message}", LogType.Run);
                return new string[0];
            }
        }

        /// <summary>
        /// 保存当前配方
        /// </summary>
        /// <returns></returns>
        public static void SaveProduct()
        {
            try
            {
                FileTool.WriteXML(AllTypeXml, ProductType);
            }
            catch (Exception ex)
            {
                Log.SaveError(ex, $"保存当前配方名异常，{ex.Message}", LogType.Run);
            }
        }
    }
}
